 /***************** Starting Video  *******************/

    var global_user_id = "";
    function createvideoelement(url){
            var videostring = "<OBJECT ID='MediaPlayer' WIDTH='120' HEIGHT='120' CLASSID='CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95' STANDBY='Loading Windows Media Player components...' TYPE='application/x-oleobject'>";
            videostring += "<PARAM NAME='FileName' VALUE='"+url+"'>";
            videostring += "<PARAM name='ShowControls' VALUE='true'>";
            videostring += "<param name='ShowStatusBar' value='false'>";
            videostring += "<PARAM name='ShowDisplay' VALUE='false'>";
            videostring += "<PARAM name='autostart' VALUE='false'>";
            videostring += "<EMBED TYPE='application/x-mplayer2' SRC='"+url+"' NAME='MediaPlayer'  WIDTH='120' HEIGHT='120' ShowControls='1' ShowStatusBar='0' ShowDisplay='0' autostart='0' CONSOLE='video'></EMBED>";
            videostring += "</OBJECT>";
            return videostring;
    }
    function maketablevideo(data){
        var strdata = "<table><thead></thead><tbody>";
        for (var i = 0; i < data.length; i++) {
            if(i%4 ==0){
                strdata += "<tr>"
            }
            strdata += "<td >"+createvideoelement(data[i].video)+"</td>";
            if(i%4 ==3){
                strdata += "</tr>"
            }
        }
        return strdata += "</tbody></table>"
    }
    function makevideo(data) {
       return maketablevideo(data);

    }
    function onUserVideoList(data) {
        var uservideo = new Array();
        if (data.data && data.data.length) {
            uservideo = data.data;
        }
        var strVstring =makevideo(uservideo);
       
        document.getElementById('videoid').innerHTML = strVstring;      
    }
    function requestDataVideo(url) {
        
        var params = {};
        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
        gadgets.io.makeRequest(url, onUserVideoList, params);
    }
   function initvideo(url) {
        requestDataVideo(url);
    }
   function callvideodiv(first_div,second_div,third_div){
        document.getElementById('videoid').innerHTML = "Loading Video's please wait";

        document.getElementById(""+first_div+"").style.display = 'block';
        document.getElementById(""+second_div+"").style.display = 'none';
        document.getElementById(""+third_div+"").style.display = 'none';
        document.getElementById(""+first_div+"_link").setAttribute("class", "active");
        document.getElementById(""+second_div+"_link").setAttribute("class", "");
        document.getElementById(""+third_div+"_link").setAttribute("class", "");
        initvideo('http://spark.tekmindz.com/cpng/rt/rocketalk.php?type=video');
        return false;
   }

   /********************* Starting Photos  ********************/
    function createimageelement(url){
        var imagestr = "<img border='1' width='80' height='80' src='"+url+"'>";
        return imagestr;

    }
    function maketableimage(data){
        var strdata = "<table cellspacing='14' cellpadding='14'><thead></thead><tbody>";
        for (var i = 0; i < data.length; i++) {
            if(i%4 ==0){
                strdata += "<tr>"
            }
            strdata += "<td >"+createimageelement(data[i].photo)+"</td>";
            if(i%4 ==3){
                strdata += "</tr>"
            }
        }
        return strdata += "</tbody></table>"
    }
    function makephotos(data) {
         return  maketableimage(data);
    }
    function onUserPhotoList(data) {
        var userphoto = new Array();
        if (data.data && data.data.length) {
            userphoto = data.data;
        }
        var strPstring =makephotos(userphoto);
        document.getElementById('photoid').innerHTML = strPstring;
    }
    function requestDataPhoto(url) {
        var params = {};
        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
        gadgets.io.makeRequest(url, onUserPhotoList, params);
    }
   function initphoto(url) {
        requestDataPhoto(url);
    }


   function callphotodiv(first_div,second_div,third_div){
        document.getElementById('photoid').innerHTML = "Loading Photo's please wait";
        document.getElementById(""+first_div+"").style.display = 'block';
        document.getElementById(""+second_div+"").style.display = 'none';
        document.getElementById(""+third_div+"").style.display = 'none';
        document.getElementById(""+first_div+"_link").setAttribute("class", "active");
        document.getElementById(""+second_div+"_link").setAttribute("class", "");
        document.getElementById(""+third_div+"_link").setAttribute("class", "");
        initphoto('http://spark.tekmindz.com/cpng/rt/rocketalk.php?type=photo');
        return false;
   }


   /********************** call for third div *********************/


    function onUserGalaryList(data) {
        var userdata = new Array();
        if (data.data && data.data.length) {
            userdata = data.data;
        }
        var strPstring =makephotos(userdata);
        var strVstring =makephotos(userdata);
        document.getElementById('galarydetailid').style.display = 'none';
        document.getElementById('galaryphotoid').style.display = 'block';
        document.getElementById('galaryvideoid').style.display = 'block';
        document.getElementById('galaryphotoid').innerHTML = strPstring;
        document.getElementById('galaryvideoid').innerHTML = strVstring;
    }
    function requestDataGalary(url) {
        var params = {};
        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
        gadgets.io.makeRequest(url, onUserGalaryList, params);
    }
    function onResponseCheckUser(data) {
       
        var userdata = new Array();
      
        responsedata = data;
       
        alert("before response");
        alert(responsedata);
        alert("status");
        alert(responsedata.status);
        alert(responsedata.data.status);
        alert("aaaaaaaaa00");
        if(responsedata.status){alert("inside sucess");
            var strPstring =makephotos(userdata);
            var strVstring =makevideo(userdata);
            document.getElementById('galarydetailid').style.display = 'none';
            document.getElementById('galaryphotoid').style.display = 'block';
            document.getElementById('galaryvideoid').style.display = 'block';
            document.getElementById('galaryphotoid').innerHTML = strPstring;
            document.getElementById('galaryvideoid').innerHTML = strVstring;
        } else {
            alert("inside failure");
            document.getElementById('galarydetailid').style.display = 'block';
            document.getElementById('galaryphotoid').style.display = 'none';
            document.getElementById('galaryvideoid').style.display = 'none';
        }    
    }
    function checkuser(url) {        
        var params = {};
        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
        gadgets.io.makeRequest(url, onResponseCheckUser, params);
    }

    function onLoadGetUserDetails(data) {
       var viewer = data.get('viewer').getData();      
       global_user_id = viewer.getId();
       var url = "http://spark.tekmindz.com/cpng/rt/rocketalk.php?type=galary";
       var url_modified = url+"&user_id="+global_user_id+"&client="+opensocial.getEnvironment().getDomain();       
       checkuser(url_modified);
    }
   function createuser(url){
         var req = opensocial.newDataRequest();
         req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER),'viewer');
         var viewerDetails = opensocial.newIdSpec({ "userId" : "VIEWER"});
         var opt_params = {};
         opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
         req.add(req.newFetchPeopleRequest(viewerDetails, opt_params),'viewerFriends');
         req.send(onLoadGetUserDetails);
         

    }

     

    function initgalary(url){        
        createuser(url);
       // var url_modified = url+"&user_id="+global_user_id+"&client="+opensocial.getEnvironment().getDomain();
      //  alert("after user");

        //check for user already registered with rocktak with this
       // checkuser(url_modified);
        //return false;
        //requestDataGalary(url_modified);
    }

   function callgalarydiv(first_div,second_div,third_div){
        document.getElementById('photoid').innerHTML = "Loading Galary please wait";
        document.getElementById(""+first_div+"").style.display = 'block';
        document.getElementById(""+second_div+"").style.display = 'none';
        document.getElementById(""+third_div+"").style.display = 'none';
        document.getElementById(""+first_div+"_link").setAttribute("class", "active");
        document.getElementById(""+second_div+"_link").setAttribute("class", "");
        document.getElementById(""+third_div+"_link").setAttribute("class", "");
        initgalary('http://spark.tekmindz.com/cpng/rt/rocketalk.php?type=galary');
        return false;
   }

   function init() {
       var url = "";
       
        requestDataVideo('http://spark.tekmindz.com/cpng/rt/rocketalk.php?type=video');
    }
    gadgets.util.registerOnLoadHandler(init);